from django.contrib import admin
from .models import City, Datacenter, Host, PasswordHistory, HostStatistics

@admin.register(City)
class CityAdmin(admin.ModelAdmin):
    list_display = ('name', 'code')
    search_fields = ('name', 'code')

@admin.register(Datacenter)
class DatacenterAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'city', 'address')
    list_filter = ('city',)
    search_fields = ('name', 'code', 'address')

@admin.register(Host)
class HostAdmin(admin.ModelAdmin):
    list_display = ('hostname', 'ip_address', 'datacenter', 'status', 'cpu_cores', 'memory_gb', 'created_at')
    list_filter = ('status', 'datacenter', 'datacenter__city', 'is_active')
    search_fields = ('hostname', 'ip_address', 'notes')
    readonly_fields = ('created_at', 'updated_at')
    fieldsets = (
        ('基本信息', {
            'fields': ('hostname', 'ip_address', 'datacenter', 'status', 'is_active')
        }),
        ('硬件规格', {
            'fields': ('cpu_cores', 'memory_gb', 'disk_gb', 'os_type')
        }),
        ('访问信息', {
            'fields': ('ssh_port', 'root_password')
        }),
        ('其他信息', {
            'fields': ('notes', 'created_at', 'updated_at')
        }),
    )

@admin.register(PasswordHistory)
class PasswordHistoryAdmin(admin.ModelAdmin):
    list_display = ('host', 'changed_at', 'changed_by')
    list_filter = ('changed_at', 'changed_by', 'host')
    search_fields = ('host__hostname', 'host__ip_address')
    readonly_fields = ('host', 'old_password', 'new_password', 'changed_at', 'changed_by')

@admin.register(HostStatistics)
class HostStatisticsAdmin(admin.ModelAdmin):
    list_display = ('date', 'city', 'datacenter', 'host_count', 'online_count', 'offline_count')
    list_filter = ('date', 'city', 'datacenter')
    readonly_fields = ('date', 'city', 'datacenter', 'host_count', 'online_count', 'offline_count', 'maintenance_count')